#!/usr/bin/env python

import sbak
from sbak import ui, event, draw

SCREENRES = 800,300

MSG = """
This example shows how sbak.ui.UI objects can be
used to generate UIEVENT events. To see event
data, look at the stdout of this program while
this window is in focus.

This also shows how to use the `sbak.event.EventManager`
class rather than an EQM, as in examples/events.py and
./gameevents.py.

Press Escape to quit.
"""

# Program entry point.
def main():
    global quitflag
    
    # Initialize the system.
    sbak.init()
    
    # Create a window.
    screen = sbak.video.set_mode(SCREENRES)
    
    # Display the message on-screen.
    sbak.draw.text_nl(
        dest = screen,
        pos = (0,0),
        text = MSG
    )
    
    # Refresh the screen.
    sbak.video.update()
    
    # Create an event manager for the UI's.
    em = sbak.event.EventManager()
    
    # Create some UI's.
    
    sbak.ui.init_joy(0)
    
    # This UI will respond to 'w' and 'up' and to the 2nd joystick axis,
    # and to the Y axis of the joy hat moving up.
    ui_up = ui.Button(
        id = 'up',
        events = em,
        keys = ('w', 'up'),
        joy_axis_threshold = (0, 1, -.1),
        joy_hat_y = (0, 0, 1)
    )
    
    # This UI will respond to 's' and 'down' and the 2nd joystick axis.
    ui_down = ui.Button(
        id = 'down',
        events = em,
        keys = ('s', 'down'),
        joy_axis_threshold = (0, 1, .1),
        joy_hat_y = (0, 0, -1)
    )
    
    # This UI will respond to left clicks.
    ui_action = ui.Button('action', em, mouse_button = 1)
    
    # Create some exit hooks.
    em.hook(quit, 'quit'),
    em.hook(quit, 'keydown', key = 'escape')
   
    # ~~~ MAIN LOOP ~~~
    
    # Run until quitflag is set.
    quitflag = False
    while not quitflag:
        
        # Get the list of most recent events from the queue.
        events = sbak.event.get()
        
        # Print all events that have occured on the system queue.
        for e in events:
            print '"' + event.get_name(e.type) + '"'
            for k,v in e.dict.iteritems():
                print '\t"%s" = %s' % (k, v)
            print
            
            # Notify the manager that the event occurred.
            em.notify(e)

def quit(e):
    global quitflag
    quitflag = True

if __name__ == '__main__':
    main()